﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Drawing;
using DevExpress.Web.ASPxPivotGrid;

public partial class WebUserControl_WebUserControl2 : System.Web.UI.UserControl
{
    
    static string error = "";
    ClassExecute.ClassExecute dt;
    string str = BCDataCenter.Configuration.CurrentConnectionString;
    static string usertype = "";
    static DataTable dataCap;
    MethodClassCommon common;
    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
        
            common = new MethodClassCommon();
            usertype = common.GetDataDepend("GET_USERTYPE", "UserName", Session["username"].ToString());
            BindToDrop();
            if (usertype == "ad")
            {
                DropTTPP.SelectedValue = Session["sitecode"].ToString();
                DropTTPP.Enabled = true;
            }
            else
            {
                DropTTPP.SelectedValue = Session["sitecode"].ToString();
                DropTTPP.Enabled = false;

            }
        }
        else
        {
            try
            {
                Title();
                this.ASPxPivotGrid1.DataSource = dataCap;
                this.ASPxPivotGrid1.DataBind();
            }
            catch
            {
                return;
            }
        }
    }
    void BindToDrop()
    {
        dt=new ClassExecute.ClassExecute();
        DataTable db = dt.Get_Data_DataTable(str,ref error,"GET_SITENAME_ALL",CommandType.StoredProcedure);
        DropTTPP.DataSource = db;
        DropTTPP.DataTextField = "sitename";
        DropTTPP.DataValueField = "Sitecode";
        DropTTPP.DataBind();
    }
    public void Title()
    {
        if (!ASPxPivotGrid1.Fields.Contains(ASPxPivotGrid1.Fields["Baocao"]))
        {
            ASPxPivotGrid1.Fields.Add("Baocao", DevExpress.XtraPivotGrid.PivotArea.FilterArea);
            ASPxPivotGrid1.Fields["Baocao"].HeaderStyle.BackColor = Color.Yellow;
            ASPxPivotGrid1.Fields["Baocao"].Caption = "Báo cáo cấp quản lý trong năm";
        }
        if (!ASPxPivotGrid1.Fields.Contains(ASPxPivotGrid1.Fields["FromDate"]))
        {
            ASPxPivotGrid1.Fields.Add("FromDate", DevExpress.XtraPivotGrid.PivotArea.FilterArea);
            ASPxPivotGrid1.Fields["FromDate"].HeaderStyle.BackColor = Color.Yellow;
            ASPxPivotGrid1.Fields["FromDate"].Caption = "TTPP : " + DropTTPP.SelectedItem.Text;
        }
        if (!ASPxPivotGrid1.Fields.Contains(ASPxPivotGrid1.Fields["Time"]))
        {
            ASPxPivotGrid1.Fields.Add("Time", DevExpress.XtraPivotGrid.PivotArea.FilterArea);
            ASPxPivotGrid1.Fields["Time"].HeaderStyle.BackColor = Color.Yellow;
            ASPxPivotGrid1.Fields["Time"].Caption = "Ngày giờ lấy báo cáo : " + DateTime.Now.ToShortDateString() + "  " + DateTime.Now.ToShortTimeString();
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {

            ASPxPivotGrid1.Fields.Clear();
            Title();
            dt = new ClassExecute.ClassExecute();
            dataCap = dt.Get_Data_DataTable(str, ref error, "K_CapQuanLyBum", CommandType.StoredProcedure, new SqlParameter("@Username", Session["username"].ToString()), new SqlParameter("@Sitecode", DropTTPP.SelectedValue.ToString()));
            foreach (DataColumn col in dataCap.Columns)
            {

                ASPxPivotGrid1.Fields.Add(col.ColumnName.ToString(), DevExpress.XtraPivotGrid.PivotArea.RowArea);

            }

            this.ASPxPivotGrid1.DataSource = dataCap;
            this.ASPxPivotGrid1.DataBind();
        }
        catch (Exception ex)
        {
            BCDataCenter.ClassWriteLogErr obj = new BCDataCenter.ClassWriteLogErr();
            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            obj.ErrorLog(ex.Message, method);
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        try
        {
            using (MemoryStream filestream = new MemoryStream())
            {

                string contentType = "application/ms-excel";
                string fileName = System.DateTime.Now.ToString("dd/MM/yyyy") + "_BaoCaoCapQuanLyMoiTrongNam.xls";
                ASPxPivotGridExporter1.ExportToXls(filestream);
                byte[] buffer = filestream.GetBuffer();
                Response.Clear();
                Response.Buffer = false;
                Response.AppendHeader("Content-Type", contentType);
                Response.AppendHeader("Content-Transfer-Encoding", "binary");
                Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName);
                Response.BinaryWrite(buffer);
                Response.End();
            }
        }
        catch (Exception ex)
        {
            BCDataCenter.ClassWriteLogErr obj = new BCDataCenter.ClassWriteLogErr();
            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            obj.ErrorLog(ex.Message, method);
        }
    }
}
